<template>
  <div>
    <iframe :src="wechatUrl" frameborder="0" style="overflow: scroll">

    </iframe>
    <div class="footer">
      <el-button class="primary_btn" @click.stop="backPreviousStep()">{{$t('public.cancel')}}</el-button>
    </div>
  </div>
</template>
<script>
  export default {
    name: 'weixinAuthStep1',
    data () {
      return {
        wechatUrl: ''
      }
    },
    methods: {
      getMessageFromChild (d) {
        if (d.data === 1) {
          this.$message.success(this.$t('configLazyLoad.channel.wexinTip114'))
          this.$emit('success')
        } else if (d.data === 2) {
          this.$message.error(this.$t('configLazyLoad.channel.wexinTip115'))
          this.$emit('error')
        } else if (d.data === 3) {
          this.$message.error(this.$t('configLazyLoad.channel.wexinTip116'))
          this.$emit('error')
        }
      },
      backPreviousStep () {
        this.$emit('preStep')
      }
    },
    beforeMount () {
      if (window.addEventListener) {
        window.addEventListener('message', this.getMessageFromChild)
      } else {
        window.attachEvent('onmessage', this.getMessageFromChild)
      }
      let accountId = this.$store.state.session.user.account
      let account = this.$store.state.session.account
      this.$store.dispatch('queryWeChatAuthorizationList', {}).then((resp) => {
        if (resp.success) {
          this.wechatUrl = resp.wechatUrl.replace('http://', '//') + '/?action=getPreAuthCode&account=' + accountId + '&appId=' + (account.wechatAppId || '')
//          this.wechatUrl = 'http://wetest.7moor.com' + '/?account=' + accountId + '&appId=' + (account.wechatAppId || '')
        }
      })
    },
    beforeDestroy () {
      window.removeEventListener('message', this.getMessageFromChild)
    }
  }
</script>

<style scoped lang="stylus">
  iframe
    width 67%
    height 430px
  .footer
    text-align center
  .primary_btn
    margin 10px 0
  /* 新版设置中微信绑定页高度需要变化 */
  .new-config
    iframe
      height calc(100vh - 200px)
</style>
